// pages/car/car.js
const app = getApp()
const util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cars:[],
    checkall:false,
    allprice:"0.00",
    allcount:0,
    isdelete:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.GoLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.Find_Car();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  Find_Car: function () {
    var that = this;
    var data = {};
    data.act = "Find_Car";
    wx.showLoading({
      title: '更新中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      that.setData({ cars: json.data});
      var checkall=true;
      for (var i = 0; i < that.data.cars.length; i++){
        if (that.data.cars[i].IsCheck==0){
          checkall=false;
        }
      }
      that.setData({ checkall: checkall });
      that.AllPrice();
    });
  },
  CheckAll:function(e){
    var cars = this.data.cars;
    for (var i = 0; i < cars.length;i++){
      if (this.data.checkall){
        cars[i].IsCheck = 0;
      }else{
        cars[i].IsCheck = 1;
      }
    }
    this.setData({ cars: cars,checkall:!this.data.checkall });
    this.AllPrice();
    this.Set_IsCheck(cars);
  },
  CheckOne: function (e) {
    var id = e.currentTarget.dataset.id;
    var cars = this.data.cars;
    this.data.checkall = true;
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].ID != id && cars[i].IsCheck==0) {
        this.data.checkall = false;
      }
      if (cars[i].ID == id && cars[i].IsCheck == 1)
      {
        this.data.checkall = false;
      }
      if (cars[i].ID == id){
        cars[i].IsCheck = cars[i].IsCheck == 0 ? 1 : 0;
      }
    }
    this.data.cars=cars;
    this.setData({checkall: this.data.checkall });
    this.AllPrice();
    this.Set_IsCheck(cars);
  },
  AddCount: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var format = e.currentTarget.dataset.format;
    var cars = this.data.cars;
    var index=-1;
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].ID == id) {
        index=i;
      }
    }
    var data = {};
    data.productid = cars[index].ProductID;
    data.count = cars[index].Count + 1;
    data.ischeck=1;
    data.flag = "update";
    data.format=format;
    data.act = "Save_Car";
    wx.showLoading({
      title: '更新中...',
    })
    app.Ajax(data, function (json) {
      cars[index].Count = data.count;
      that.setData({ cars: cars});
      that.AllPrice();
      wx.hideLoading();
    });
  },
  MinusCount: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var format = e.currentTarget.dataset.format;
    var cars = this.data.cars;
    var index = -1;
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].ID == id) {
        index = i;
      }
    }

    var data = {};
    data.productid = cars[index].ProductID;
    if (cars[index].Count==1){
      app.showError("数量不能小于1");
      return;
    }else{
      data.count = cars[index].Count - 1;
    }
    data.format = format;
    data.ischeck = 1;
    data.flag = "update";
    data.act = "Save_Car";
    wx.showLoading({
      title: '更新中...',
    })
    app.Ajax(data, function (json) {
      cars[index].Count = data.count;
      that.setData({ cars: cars });
      that.AllPrice();
      wx.hideLoading();
    });
  },
  SetCount: function (e) {
    var that = this;
    var cars = this.data.cars;
    if (isNaN(e.detail.value) || e.detail.value==''){
      app.showError("请输入数字");
      that.setData({ cars: cars });
      return;
    }
    var id = e.currentTarget.dataset.id;
    var format = e.currentTarget.dataset.format;
    var count = parseInt(e.detail.value);
    
    var index = -1;
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].ID == id) {
        index = i;
      }
    }
    var data = {};
    data.productid = cars[index].ProductID;
    if (count == 0) {
      app.showError("数量不能小于1");
      that.setData({ cars: cars });
      return;
    } else {
      data.count = count;
    }
    data.format = format;
    data.ischeck = 1;
    data.flag = "update";
    data.act = "Save_Car";
    wx.showLoading({
      title: '更新中...',
    })
    app.Ajax(data, function (json) {
      cars[index].Count = data.count;
      that.setData({ cars: cars });
      that.AllPrice();
      wx.hideLoading();
    });
  },
  AllPrice: function(){
    var cars = this.data.cars;
    var allprice=0;
    var allcount=0;
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].IsCheck==1) {
        allprice += cars[i].APrice*cars[i].Count;
        allcount += cars[i].Count
      }
    }
    if(allcount>0){
      this.isdelete="select";
    }else{
      this.isdelete = "";
    }
    this.setData({ allprice: allprice.toFixed(2) + "", allcount: allcount, isdelete: this.isdelete});
  },
  Delete_Car:function(){
    var that = this;
    var cars = that.data.cars;
    var ids=new Array();
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].IsCheck == 1) {
        ids.push(cars[i].ID);
      }
    }
    if(ids.length==0)
    {
      return false;
    }
    wx.showModal({
      title: '提示',
      confirmColor: '#fb4747',
      content: '您确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          var data = {};
          data.ids = ids.join(",");
          data.act = "Delete_Car";
          wx.showLoading({
            title: '删除中...',
          })
          app.Ajax(data, function (json) {
            wx.hideLoading();
            app.showSuccess(json.msg);
            //倒向删除，否则会有错
            for (var i = cars.length-1; i >= 0 ; i--) {
              if (cars[i].IsCheck == 1) {
                cars.Remove(cars[i]);
              }
            }
            that.setData({ cars: cars });
            that.AllPrice();
          });
        }
      }
    })
  },
  Set_IsCheck:function(cars){
    var ids = new Array();
    for (var i = 0; i < cars.length; i++) {
      if (cars[i].IsCheck == 1) {
        ids.push(cars[i].ID);
      }
    }
    var data = {};
    data.ids = ids.join(",");
    data.act = "Set_IsCheck";
    wx.showLoading({
      title: '更新中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();    
    });
  },
  openProductInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/productinfo/productinfo?id=' + id
    })
  },
  buy:function(e){
    if (this.data.allcount==0){
      app.showError("请选择结算商品");
      return;
    }
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/pay/pay?id=' + id + "&from=car"
    })
  },
  openProduct:function(){
    app.IsFlash = true;
    wx.navigateTo({
      url: '/pages/product/product?order=allrank'
    })
  }

})